//
//  BuyOptionsView.m
//  HDYS
//
//  Created by kaynewbie on 15/4/24.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "BuyOptionsView.h"
#import "ColorCell.h"
#import "SizeCell.h"
#import "NumCell.h"

#import "UIImageView+WebCache.h"
#import "Header.h"

#define CELLHEIGHT 80
#define CELLNUM 3

@interface BuyOptionsView () <UITableViewDataSource, UITableViewDelegate, SizeCellDelegate, NumCellDelegate>
{
    MainDataModel *_model;
    
    UIImageView *_iconImgView;
    UILabel *_nameLabel;
    UILabel *_priceLabel;
    
    NSString *_chosenSize;
    NSString *_chosenNum;
}
@end

@implementation BuyOptionsView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
    _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 70, 70)];
//    iconImgView.image = [UIImage imageNamed:@"image_edit_filter_item_image.png"];
    [topView addSubview:_iconImgView];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconImgView.frame.origin.x + _iconImgView.frame.size.width + 5, _iconImgView.frame.origin.y + 10, 150, 25)];
    _nameLabel.numberOfLines = 0;
    _nameLabel.font = [UIFont systemFontOfSize:11];
    [topView  addSubview:_nameLabel];
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10, 60, 15)];
    _priceLabel.textAlignment = NSTextAlignmentCenter;
//    priceLabel.text = @"💲68";
    _priceLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_priceLabel];
    
    CGFloat btnW = 50.0f;
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(SCREEN_WIDTH - 10 - btnW, _iconImgView.frame.origin.x, btnW, 15);
    [backBtn setTitle:@"取消" forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [backBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, topView.frame.size.height - 0.5, SCREEN_WIDTH, 0.5)];
    lineView1.backgroundColor = [UIColor grayColor];
    [topView addSubview:lineView1];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topView.frame.size.height, SCREEN_WIDTH, CELLHEIGHT * CELLNUM + 20) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self addSubview:tableView];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(tableView.frame.origin.x, tableView.frame.size.height + tableView.frame.origin.y , SCREEN_WIDTH, 45)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bottomView];
    
    CGFloat commitBtnW = 100.0f;
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.frame = CGRectMake((SCREEN_WIDTH - commitBtnW) / 2, 10, commitBtnW, bottomView.frame.size.height - 20);
    [commitBtn setTitle:@"确定" forState:UIControlStateNormal];
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [commitBtn setBackgroundImage:[UIImage imageNamed:@"btn_confirm_87x36@2x"] forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:commitBtn];
}

#pragma mark - 协议
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return CELLNUM;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString *cellIdentifier = @"colorOptionCell";
        ColorCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ColorCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    } else if (indexPath.row == 1) {
        static NSString *cellIdentifier = @"sizeOptionCell";
        SizeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[SizeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            //选择尺寸代理
            cell.delegate = self;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    } else {
        static NSString *cellIdentifier = @"numOptionCell";
        NumCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[NumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return CELLHEIGHT + 20;
    } else {
        return CELLHEIGHT;
    }
}

- (void)config:(MainDataModel *)m {
    _model = m;
    
    [_iconImgView setImageWithURL:[NSURL URLWithString:[URL_PRE stringByAppendingString:m.pic]]];
    _nameLabel.text = m.gname;
    _priceLabel.text = m.price;
}


#pragma mark - 返回
-(void)backBtnClick{
    //取消购买
    if ([self.delegate performSelector:@selector(BuyOptionsViewCancle)]) {
        [self.delegate BuyOptionsViewCancle];
    }
}

//提交订单
-(void)commitBtnClick{
    if ([self.delegate respondsToSelector:@selector(BuyOptionsViewWillBuyGoodsWithSize:count:)]) {
        [self.delegate BuyOptionsViewWillBuyGoodsWithSize:_chosenSize count:_chosenNum];
    }
}

#pragma mark - SizeCellDelegate代理
- (void)chooseSize:(NSString *)size {
    _chosenSize = size;
}

#pragma mark - NumCellDelegate代理
- (void)chooseNum:(NSString *)num {
    _chosenNum = num;
}

@end
